/**
 * Created by Administrator on 2014/8/7.
 */

function resize_canvas(){
    var center_data_json = {
        id: "center",
        width: 100,
        height: 200,
        fill: "white"
    }
    var head_data_json = {
        id: "head",
        height: 20,
        fill: "red"
    }
    var foot_data_json = {
        id: "foot",
        height: 100,
        fill: "red"
    }
    var side_data_json = {
        id: "side",
        width: 50,
        fill: "blue"
    }

    client.center_data = center_data_json;
    client.head_data = head_data_json;
    client.foot_data = foot_data_json;
    client.side_data = side_data_json;

    initStage();

    initData();

    addAllRect();

    addMark();
}



function drawMark(ctx) {
    ctx.beginPath();
    ctx.lineWidth = 1;
    // 头部宽
    ctx.moveTo(config.HEAD.x, config.HEAD.y - 10);
    ctx.lineTo(config.RIGHT.x + config.RIGHT.width, config.HEAD.y - 10);
    ctx.moveTo(config.HEAD.x, config.HEAD.y - 15);
    ctx.lineTo(config.HEAD.x, config.HEAD.y - 5);
    ctx.moveTo(config.RIGHT.x + config.RIGHT.width, config.HEAD.y - 15);
    ctx.lineTo(config.RIGHT.x + config.RIGHT.width, config.HEAD.y - 5);
    // 边的宽
    ctx.moveTo(config.FOOT.x, config.FOOT.y + config.FOOT.height + 10);
    ctx.lineTo(config.RIGHT.x, config.FOOT.y + config.FOOT.height + 10);
    ctx.moveTo(config.FOOT.x, config.FOOT.y + config.FOOT.height + 5);
    ctx.lineTo(config.FOOT.x, config.FOOT.y + config.FOOT.height + 15);
    ctx.moveTo(config.RIGHT.x, config.FOOT.y + config.FOOT.height + 5);
    ctx.lineTo(config.RIGHT.x, config.FOOT.y + config.FOOT.height + 15);
    // 中央的宽
    ctx.moveTo(config.CENTER.x, config.FOOT.y + config.FOOT.height + 10);
    ctx.lineTo(config.RIGHT.x, config.FOOT.y + config.FOOT.height + 10);
    ctx.moveTo(config.FOOT.x, config.FOOT.y + config.FOOT.height + 5);
    ctx.lineTo(config.FOOT.x, config.FOOT.y + config.FOOT.height + 15);
    ctx.moveTo(config.LEFT.x + config.LEFT.width, config.FOOT.y + config.FOOT.height + 5);
    ctx.lineTo(config.LEFT.x + config.LEFT.width, config.FOOT.y + config.FOOT.height + 15);
    // 边的高
    ctx.moveTo(config.LEFT.x - 10, config.LEFT.y);
    ctx.lineTo(config.LEFT.x - 10, config.LEFT.y + config.LEFT.height);
    ctx.moveTo(config.LEFT.x - 15, config.LEFT.y);
    ctx.lineTo(config.LEFT.x - 5, config.LEFT.y);
    ctx.moveTo(config.LEFT.x - 15, config.LEFT.y + config.LEFT.height);
    ctx.lineTo(config.LEFT.x - 5, config.LEFT.y + config.LEFT.height);
    // 天的高
    ctx.moveTo(config.HEAD.x - 10, config.HEAD.y);
    ctx.lineTo(config.HEAD.x - 10, config.HEAD.y + config.HEAD.height);
    ctx.moveTo(config.HEAD.x - 15, config.HEAD.y);
    ctx.lineTo(config.HEAD.x - 5, config.HEAD.y);
    ctx.moveTo(config.HEAD.x - 15, config.HEAD.y + config.HEAD.height);
    ctx.lineTo(config.HEAD.x - 5, config.HEAD.y + config.HEAD.height);
    // 地的高
    ctx.moveTo(config.FOOT.x - 10, config.FOOT.y);
    ctx.lineTo(config.FOOT.x - 10, config.FOOT.y + config.FOOT.height);
    ctx.moveTo(config.FOOT.x - 15, config.FOOT.y);
    ctx.lineTo(config.FOOT.x - 5, config.FOOT.y);
    ctx.moveTo(config.FOOT.x - 15, config.FOOT.y + config.FOOT.height);
    ctx.lineTo(config.FOOT.x - 5, config.FOOT.y + config.FOOT.height);

    // 数字标示
    ctx.fillText(getNum(changeUnit(config.HEAD.width)), config.HEAD.x + config.HEAD.width/2 - 10, config.HEAD.y - 15);
    ctx.fillText(getNum(changeUnit(config.LEFT.width)), config.LEFT.x + config.LEFT.width/2 - 10, config.FOOT.y + config.FOOT.height + 25);
    ctx.fillText(getNum(changeUnit(config.CENTER.width)), config.CENTER.x + config.CENTER.width/2 - 10, config.FOOT.y + config.FOOT.height + 25);
    ctx.fillText(getNum(changeUnit(config.HEAD.height)), config.HEAD.x - 35, config.HEAD.y + config.HEAD.height/2 + 5);
    ctx.fillText(getNum(changeUnit(config.FOOT.height)), config.FOOT.x - 35, config.FOOT.y + config.FOOT.height/2 + 5);
    ctx.fillText(getNum(changeUnit(config.LEFT.height)), config.LEFT.x - 35, config.LEFT.y + config.LEFT.height/2 + 5);

    ctx.closePath();
    ctx.stroke(); // 进行绘制
}

function drawAll() {
    client.stage.destroyChildren();
    addAllRect();
    addMark();
}

function changNum(num){
    switch (config.TYPE) {
        case 0:
            client.head_data.height = num;
            break;
        case 1:
            client.foot_data.height = num;
            break;
        case 2:
            client.side_data.width = num;
            break;
    }

    initData();

    drawAll();

}

function changType(type){
    config.TYPE = type;
    //初始化赋值
    switch (type) {
        case 0:
            $('#nums').val(client.head_data.height).slider("refresh");
            break;
        case 1:
            $('#nums').val(client.foot_data.height).slider("refresh");
            break;
        case 2:
            $('#nums').val(client.side_data.width).slider("refresh");
            break;
    }
}

function changIsMark(isMark){
    config.IS_MARK = isMark;
    if(isMark == 0){
        client.mark_layer.hide();
    }else{
        client.mark_layer.show();
        drawAll();
    }
}

function changNumType(type){
    config.CURRENT_UNIT = type;
    changIsMark(config.IS_MARK);
}

function reset(){
    resize_canvas();
}
//
//function saveHeadHeight(num){
//    window.save.saveHead(num);
//}
//
//function saveFootHeight(num){
//    window.save.saveFoot(num);
//}
//
function selectPic(){
    window.android.selectPic();
}

function getCenterSrc(){
    return window.android.getPicUrl();
}

function getNum(num){
    var result = "";
    var first = 0;
    var remaind = num;
    for(var i=3; i>0; i--){
        if(remaind >= Math.pow(10,i)){
            first = Math.floor(remaind / Math.pow(10,i));
            remaind = remaind % Math.pow(10,i);
            result = result +  first + unit(i);
        }
    }
    if(remaind > 0){
        result = result  + remaind + unit(0);
    }
    return result;
}

function changeUnit(num){
    if(config.CURRENT_UNIT == config.UNIT_CHI){
        return num;
    }else if(config.CURRENT_UNIT == config.UNIT_METER){
        return num / config.UNIT_RATIO;
    }
}
function unit(i){
    var result = "";
    if(config.CURRENT_UNIT == config.UNIT_CHI){
        switch (i) {
            case 2:
                result = "尺";
                break
            case 1:
                result = "寸";
                break
            case 0:
                result = "分";
                break
        }
    }else if(config.CURRENT_UNIT == config.UNIT_METER){
        switch (i) {
            case 2:
                result = "米";
                break
            case 1:
                result = "分米";
                break
            case 0:
                result = "厘米";
                break
        }
    }

    return result;
}

function saveAll(){
    var saveObject = {
        head : config.HEAD,
        foot: config.FOOT,
        left: config.LEFT,
        right: config.RIGHT,
        center: config.CENTER
    }
    window.android.saveAll(JSON.stringify(saveObject));
}